package com.fy.module.exam.controller;

import com.fy.common.dto.PageDto;
import com.fy.common.vo.ApiRest;
import com.fy.common.vo.PageResult;
import com.fy.module.exam.dto.ExamSaveDto;
import com.fy.module.exam.entity.Exam;
import com.fy.module.exam.service.IExamService;
import com.fy.module.qu.dto.QuSaveDto;
import com.fy.module.repo.entity.Repo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author yang
 * @since 2023-03-22
 */
@RestController
@RequestMapping("/exam")
@Api(description = "考试模块")
@CrossOrigin
public class ExamController {

    @Autowired
    private IExamService examService;

    @PostMapping("/save")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_COMMON')")
    @ApiOperation("添加考试宗卷")
    public ApiRest save(@RequestBody ExamSaveDto examSaveDto){
        return examService.create(examSaveDto);
    }

    @PostMapping("page")
    @ApiOperation("分页查询")
    public PageResult<Exam> findPage(@RequestBody PageDto<Exam> pageDto){

        return examService.findPage(pageDto);
    }

    @DeleteMapping("/del")
    @PreAuthorize(("hasAnyRole('ROLE_ADMIN')"))
    @ApiOperation("根据id删除某个考试宗卷")
    public ApiRest delOne(String examId){
        boolean flag  = examService.removeById(examId);
        if(!flag) return ApiRest.failure();
        return ApiRest.success();
    }

    @DeleteMapping("/dels")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    @ApiOperation("根据列表id删除所有考试宗卷")
    @Transactional
    public ApiRest delBatch(@RequestBody List<String> examIds){
        boolean flag = examService.removeBatchByIds(examIds);
        if(!flag) return ApiRest.failure();
        return ApiRest.success();
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    @ApiOperation("根据列表id删除所有考试宗卷")
    @Transactional
    public ApiRest getById(@PathVariable String id){
        return examService.getExamById(id);
    }

}
